package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.innovation.chengzhangriji.R;

import java.util.HashMap;

public class HomeTabBar extends FrameLayout implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private int checked = 0;

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(onCheckChangeListener != null) {
            onCheckChangeListener.onCheckChanged(maps.get(checkedId));
        }
    }

    public static interface OnCheckChangeListener {
        public void onCheckChanged(int position);
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return onCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    private OnCheckChangeListener onCheckChangeListener;

    public HomeTabBar(Context context) {
        super(context);
    }

    public HomeTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    HashMap<Integer, Integer> maps = new HashMap<>();

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.layout_home_tab_bar, this);
        radioGroup = (RadioGroup) findViewById(R.id.tabs);
        radioGroup.check(R.id.tab_czrj);
        radioGroup.setOnCheckedChangeListener(this);
        for(int i = 0; i < radioGroup.getChildCount(); i++) {
            maps.put(radioGroup.getChildAt(i).getId(), i);
        }
    }
}
